package ltd.sumfsy.grain.service.impl;

import ltd.sumfsy.grain.bean.ULog;
import ltd.sumfsy.grain.bean.example.ULogExample;
import ltd.sumfsy.grain.mapper.ULogMapper;
import ltd.sumfsy.grain.service.ULogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户日志操作记录服务实现类
 * @author 范颂扬
 * @create 2021-05-13 17:27
 */
@Service
public class ULogServiceImpl implements ULogService {

    private final ULogMapper uLogMapper;

    @Autowired
    public ULogServiceImpl(ULogMapper uLogMapper) {
        this.uLogMapper = uLogMapper;
    }

    @Override
    public boolean save(ULog uLog) {
        int res = uLogMapper.insertSelective(uLog);
        return res > 0;
    }

    @Override
    public boolean deleteById(Integer id) {
        int res = uLogMapper.updateByPrimaryKeySelective(new ULog().setId(id).setDelFlag(1));
        return res > 0;
    }

    @Override
    public boolean deleteByUserId(Integer userId) {
        ULogExample example = new ULogExample();
        example.createCriteria().andDelFlagEqualTo(0).andUserIdEqualTo(userId);
        int res = uLogMapper.updateByExampleSelective(new ULog().setDelFlag(1), example);
        return res > 0;
    }

    @Override
    public List<ULog> queryAll() {
        ULogExample example = new ULogExample();
        example.createCriteria().andDelFlagEqualTo(0);
        return uLogMapper.selectByExample(example);
    }

    @Override
    public List<ULog> queryByUserId(Integer userId) {
        ULogExample example = new ULogExample();
        example.createCriteria().andDelFlagEqualTo(0).andUserIdEqualTo(userId);
        return uLogMapper.selectByExample(example);
    }
}
